<?php
/**
 * 后台登陆
 * Created by PhpStorm.
 * User: m1514
 * Date: 2016/12/26
 * Time: 10:29
 */

namespace Common\Model;


class UserModel extends BaseModel
{
    protected $pk = 'aid';
    protected $tableName = 'admin';
    protected $_validate = [
        array('adminname', 'require', '请输入用户名...'),
        array('adminpwd', 'require', '请输入密码...')
    ];

    //验证码验证方法
    Public function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
    //登陆验证
    public function Login()
    {


        if(!$this->create()) return false;
        //获取post提交数据
        $user = I('post.');
        $code = I('code');
        $id='';
//        //用户名验证
        $data=$this->where('adminname')->select(1);
        if($user['adminname']!=$data['0']['adminname']){
            $this->error='用户名错误';

            return false;
        }
        //密码验证
        if(md5($user['adminpwd'])!=$data['0']['adminpwd']){
            $this->error='密码输入错误';
            return false;
        }
        //验证码验证
        if(! $this->check_verify($code, $id)){
            $this->error='验证码错误';
            return false;
        }
        $_SESSION['admin']['aid'] = $data['0']['aid'];
        $_SESSION['admin']['adminname'] = $data['0']['adminname'];

        return true;
    }

}